package cate.game.activity.moonfight;

import cate.common.table.d.GDFunc;
import cate.game.GameBody;
import cate.game.activity.base.core.ActivityGlobalData;
import cate.game.play.part.PlaySide;
import cate.game.rank.po.Rank;
import cate.game.role.Role;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class MoonFightGlobalData extends ActivityGlobalData {
    @NoteField("key：阶段，value：阶段阵容")
    public Map<Integer, MoonFightRound> roundMap;

    @Override
    public void initialize() {
        super.initialize();
        if (roundMap == null) {
            roundMap = new HashMap<>();
        }
        roundMap.values().forEach(MoonFightRound::initialize);
    }

    public void onActOpen(GameBody game) {
        //初始化擂主
        List<Rank> ranks = game.rank.power.getRankList(null);
        for (int i = 0; i < 6; i++) {
            MoonFightRound round = new MoonFightRound();
            round.initialize();
            for (int j = 0; j < Math.min(7, ranks.size()); ++j) {
                Role role = game.role.getRole(ranks.get(j).owner.uid, true);
                MoonFightBoss boss = new MoonFightBoss();
                boss.uid = role.getUid();
                EcResult<PlaySide> r = game.fight.side.createPlaySideByRole(role, GDFunc.ADVENTURE);
                if (!r.ok()) {
                    log.error("月下比武：创建擂主阵容失败，擂主ID：{}", role.getUid());
                    continue;
                }
                boss.ps = r.data;
                round.bossMap.put(j + 1, boss);
            }
            roundMap.put(i + 1, round);
        }
    }

    public synchronized void updateBoss(int roundKey, int rank, String uid, PlaySide ps) {
        MoonFightRound round = roundMap.get(roundKey);
        if (round == null) {
            return;
        }
        MoonFightBoss boss = round.bossMap.get(rank);
        if (boss == null) {
            return;
        }
        int r = round.bossMap.entrySet().stream().filter(e -> StringUtils.equals(e.getValue().uid, uid)).map(Map.Entry::getKey).findAny().orElse(0);
        //如果挑战者不是擂主，直接替换
        if (r == 0) {
            boss.uid = uid;
            boss.ps = ps;
        } else if (r > rank) {
//            如果挑战者在榜上，但位置靠后，互换位置
            String uid1 = boss.uid;
            PlaySide ps1 = boss.ps;
            boss.uid = uid;
            boss.ps = ps;
            MoonFightBoss boss1 = round.bossMap.get(r);
            boss1.uid = uid1;
            boss1.ps = ps1;
        }


    }

    public synchronized MoonFightBoss getBoss(int roundKey, int num) {
        MoonFightBoss res = new MoonFightBoss();
        MoonFightRound round = roundMap.get(roundKey);
        if (round == null) {
            return null;
        }
        MoonFightBoss boss = round.bossMap.get(num);
        if (boss == null) {
            return null;
        }
        res.uid = boss.uid;
        res.ps = boss.ps.copy();
        return res;
    }

    @Override
    public void clear() {
        roundMap.clear();
    }
}
